import axios from 'axios'
import { ElMessageBox, ElNotification } from 'element-plus'
import { useTokenStore } from '@/stores/user'
import router from '@/router'


const http = axios.create({
    baseURL: "http://localhost:9090",
    
    timeout: 5000  
})


//请求拦截器
http.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    const store = useTokenStore();
    console.log("请求拦截");
    
    console.log(store.nickname);
    if(store.Authorization){
        config.headers.Authorization = store.Authorization;
        console.log("Authorization:" + config.headers.Authorization);

        console.log("添加Authorization Token");
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });


// 添加响应拦截器
http.interceptors.response.use(async function (response) {
    // 对响应数据做点什么
    const res = response.data;
    
    console.log("响应拦截");
    if(res.code != 200){
        switch(res.code)
        {
          //EMAIL_SEND_FAIL
          case 202:
            console.log("邮箱验证码发送失败");
            ElNotification({
              title: '验证码',
              message: '验证码发送失败，请检查邮箱是否输入正确',
              type: 'error',
            })
            return Promise.reject(new Error('邮箱验证码发送失败')); 

            break;
          //BEING_BLACKLISTED
          case 203:
            console.log("请求验证码过于频繁");
            ElNotification({
              title: '验证码',
              message: '请求验证码过于频繁，系统拉黑5小时处理',
              type: 'error',
            })
            return Promise.reject(new Error('请求验证码过于频繁')); 

            break;
          //VERIFICATION_CODE_EXPIRED
          case 211:
            console.log("失效验证码");
            ElNotification({
              title: '验证码',
              message: '验证码已失效',
              type: 'error',
            })
            return Promise.reject(new Error('失效的验证码')); 

            break;
          //VERIFICATION_CODE_ERROR
          case 212:
            console.log("VERIFICATION_CODE_ERROR");
            ElNotification({
              title: '验证码',
              message: '验证码错误',
              type: 'error',
            })
            return Promise.reject(new Error('验证码错误')); 

            break;
          //EMAIL_REGISTERED
          case 213:
            console.log("EMAIL_REGISTERED");
            ElNotification({
              title: '注册',
              message: '该邮箱已被注册',
              type: 'error',
            })
            return Promise.reject(new Error('邮箱已被注册')); 

            break;
          //WRONG_EMAIL_OR_PASSWORD
          case 221:
            console.log("WRONG_EMAIL_OR_PASSWORD");
            ElNotification({
              title: '登录',
              message: '账号或密码错误',
              type: 'error',
            })
            return Promise.reject(new Error('邮箱或密码错误')); 
            break;
          //WECHAT_ACCOUNT_NOT_BIND
          case 231:
            console.log("微信未绑定账号");
            try{
              await ElMessageBox.alert('微信未绑定账号', '绑定账号', {
                confirmButtonText: '绑定账号',
              });
              router.push('/bind_account');

              return Promise.reject(new Error('微信未绑定账号')); 

            }catch(error){
              console.log('error:' + error);
              return Promise.reject(new Error('error in wechat_not_bind')); 
            }
            
            break;
          //WECHAT_BIND_FAIL
          case 232:
            console.log("WECHAT_BIND_FAIL");
            ElNotification({
              title: '微信绑定',
              message: '绑定失败，请核实邮箱与密码',
              type: 'error',
            })
            return Promise.reject(new Error('绑定失败，请核实邮箱与密码')); 
            break;
          //TOKEN_EXPIRED_OR_INVALID
          case 241:
            console.log("登录凭证失效");
            localStorage.clear();

            try{
              await ElMessageBox.alert('登录凭证失效，请重新登陆', '凭证失效', {
                confirmButtonText: '重新登陆',
              })
              router.push('/login');
  
              return Promise.reject(new Error('登录凭证失效，请重新登录')); 

            }catch(error){
              return Promise.reject(new Error('error in TOKEN_EXPIRED_OR_INVALID ')); 

            }
            break;
          //ORIGINAL_PASSWORD_INCORRECT
          case 251:
            console.log("ORIGINAL_PASSWORD_INCORRECT");
            ElNotification({
              title: '修改密码',
              message: '原始密码错误',
              type: 'error',
            })
            return Promise.reject(new Error('原始密码错误'));
            break;
        }

    }
    else{
        return res;
    }
  }, function (error) {
    // 对响应错误做点什么
    console.log('响应拦截err' + error)

    return Promise.reject(error);
  });


export default http;

